package com.springcloud.demo.dynamicTable.autocode.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class StringUtils extends org.apache.commons.lang3.StringUtils{
    public static Logger log = LoggerFactory.getLogger(StringUtils.class);
    public static final ObjectMapper objectMapper = new ObjectMapper();

    private static final Pattern INT_PATTERN = Pattern.compile("^[-\\+]?[\\d]*$");
    private static final Pattern DOUBLE_PATTERN = Pattern.compile("^[-\\+]?[.\\d]*$");
    private static final Pattern SEARCH_TRIM =Pattern.compile("^([_%]*)(.*?)([_%]*)$");


    /**
     * 判断字符串是否为Double类型
     * */
    public static boolean isDouble(String str) {
        return DOUBLE_PATTERN.matcher(str).matches();
    }

    /**
     * 驼峰转成下划线形式
     * @param parm
     * @return
     */
    public static String camelToUnderline(String parm){
        char[] ch= parm.toCharArray();
        StringBuffer s = new StringBuffer();
        for(int i=0;i<ch.length;i++){
            if((int)ch[i] >= 65 && (int)ch[i] <= 90){
                ch[i]+=32;
                s.append("_").append(ch[i]);
            }else{
                s.append(ch[i]);
            }
        }
        return s.toString();
    }

    /**
     * 查询条件过滤开头结尾的%和_
     * */
    public static String searchTrim(String s){
        if (isEmpty(s)) {
            return s;
        }

        Matcher matcher = SEARCH_TRIM.matcher(s);
        if (matcher.find()) {
            String startStr = isBlank(matcher.group(1)) ? "" : replaceSearchStr(matcher.group(1));
            String infoStr = isBlank(matcher.group(2)) ? "" : matcher.group(2);
            String endStr = isBlank(matcher.group(3)) ? "" : replaceSearchStr(matcher.group(3));
            return startStr + infoStr + endStr;
        } else {
            return s;
        }
    }

    private static String replaceSearchStr(String s) {
        if (isEmpty(s)) {
            return s;
        }
        return s.replaceAll("%", "\\\\%").replaceAll("_", "\\\\_");
    }

    public StringUtils() {
    }

    public static String tranArrayToStr(String[] str) {
        return str != null && str.length > 0 ? str[0] : "";
    }

    public static String tranString(Object obj) {
        return obj != null ? obj.toString() : "";
    }

    public static Integer tranInteger(Object obj) {
        Integer intObj = null;

        try {
            intObj = obj != null && obj.toString().length() > 0 ? Integer.valueOf(obj.toString()) : 0;
        } catch (Exception var3) {
            var3.printStackTrace();
            intObj = 0;
        }

        return intObj;
    }

    public static Double tranDouble(Object obj) {
        Double doubleObj = null;

        try {
            doubleObj = obj != null && obj.toString().length() > 0 ? Double.valueOf(obj.toString()) : 0.0D;
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return doubleObj;
    }

    public static Float tranFloat(Object obj) {
        Float floatObj = null;

        try {
            floatObj = obj != null && obj.toString().length() > 0 ? Float.valueOf(obj.toString()) : 0.0F;
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return floatObj;
    }

    public static Long tranLong(Object obj) {
        Long result = null;

        try {
            result = obj != null && obj.toString().length() > 0 ? Long.valueOf(obj.toString()) : null;
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return result;
    }

    public static boolean checkParam(Object... objects) {
        if (null == objects) {
            return false;
        } else {
            Object[] var1 = objects;
            int var2 = objects.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object obj = var1[var3];
                if (null == obj) {
                    return false;
                }

                if (obj instanceof String) {
                    if (obj.toString().trim().equals("")) {
                        return false;
                    }
                } else if (obj instanceof Collection) {
                    if (((Collection)obj).isEmpty()) {
                        return false;
                    }
                } else if (obj instanceof Map && ((Map)obj).isEmpty()) {
                    return false;
                }
            }

            return true;
        }
    }

    public static boolean checkParamNull(Object... objects) {
        if (null == objects) {
            return false;
        } else {
            Object[] var1 = objects;
            int var2 = objects.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                Object obj = var1[var3];
                if (null != obj) {
                    return true;
                }
            }

            return false;
        }
    }

    public static boolean checkCollection(Collection collection) {
        return collection != null && !collection.isEmpty();
    }

    public static boolean isInteger(String str) {
        boolean result = true;
        int i = str.length();

        while(true) {
            --i;
            if (i < 0) {
                return result;
            }

            if (!Character.isDigit(str.charAt(i))) {
                result = false;
            }
        }
    }

    public static boolean isNotEmpty(String str) {
        return str != null && !"".equals(str);
    }

    public static List<Map<String, Object>> createList(String id, String[] array) {
        List<Map<String, Object>> list = new ArrayList();
        if (array != null && array.length > 0) {
            for(int i = 0; i < array.length; ++i) {
                if (array[i].length() > 0) {
                    Map<String, Object> linkMap = new HashMap();
                    linkMap.put(id, array[i]);
                    list.add(linkMap);
                }
            }
        }

        return list;
    }

    public static List<Map<String, Object>> createList(String id, List<String> array) {
        List<Map<String, Object>> list = new ArrayList();
        if (array != null && array.size() > 0) {
            for(int i = 0; i < array.size(); ++i) {
                if (((String)array.get(i)).length() > 0) {
                    Map<String, Object> linkMap = new HashMap();
                    linkMap.put(id, array.get(i));
                    list.add(linkMap);
                }
            }
        }

        return list;
    }

    public static String[] listToArray(List<String> list) {
        String[] array = new String[list.size()];

        for(int i = 0; i < list.size(); ++i) {
            array[i] = (String)list.get(i);
        }

        return array;
    }

    public static Map<String, Object> createMap(String key, Object value) {
        Map<String, Object> map = createMap();
        map.put(key, value);
        return map;
    }

    public static Map<String, Object> createMap() {
        Map<String, Object> map = new HashMap();
        return map;
    }

    public static String additionalNum(int num, Integer length) {
        StringBuffer numStr = new StringBuffer(String.valueOf(num));
        if (numStr.length() < length) {
            for(int i = numStr.length(); i < length; ++i) {
                numStr.insert(0, "0");
            }
        }

        return numStr.toString();
    }

    public static String toJSONString(Object obj) {
        try {
            if (obj != null) {
                return objectMapper.writeValueAsString(obj);
            }
        } catch (JsonProcessingException var2) {
            log.error(var2.getMessage());
        }

        return "";
    }

    public static long getTime(Date date, boolean isBig) {
        return date == null ? (isBig ? 9223372036854775807L : -1L) : date.getTime();
    }

    public static boolean contains(String source, String... strings) {
        if (strings != null && strings.length > 0) {
            String[] var2 = strings;
            int var3 = strings.length;

            for(int var4 = 0; var4 < var3; ++var4) {
                String str = var2[var4];
                if (source.contains(str)) {
                    return true;
                }
            }

            return false;
        } else {
            return true;
        }
    }

    public static boolean equals(String source, String... strings) {
        if (strings != null && strings.length > 0) {
            if (source == null) {
                return false;
            } else {
                String[] var2 = strings;
                int var3 = strings.length;

                for(int var4 = 0; var4 < var3; ++var4) {
                    String str = var2[var4];
                    if (source.equals(str)) {
                        return true;
                    }
                }

                return false;
            }
        } else {
            return true;
        }
    }

    public static String getUUID() {
        String uuid = UUID.randomUUID().toString();
        return uuid.replaceAll("-", "");
    }

    public static String getUUID(String code) {
        String uuid = getUUID();
        if (code != null) {
            uuid = code + uuid;
        }

        return uuid;
    }

    public static String tranMap2Xml(Map<String, Object> map) {
        StringBuffer result = new StringBuffer();
        if (checkParam(map)) {
            Iterator var2 = map.entrySet().iterator();

            while(var2.hasNext()) {
                Map.Entry<String, Object> entry = (Map.Entry)var2.next();
                String key = (String)entry.getKey();
                Object value = entry.getValue();
                result.append(tran2Xml(key, value));
            }
        }

        return result.toString();
    }

    public static String tran2Xml(String key, Object value) {
        StringBuffer result = new StringBuffer();
        result.append("<" + key + ">");
        if (value instanceof Map) {
            Map<String, Object> valueMap = (Map)value;
            if (checkParam(valueMap)) {
                Iterator var4 = valueMap.entrySet().iterator();

                while(var4.hasNext()) {
                    Map.Entry<String, Object> entry = (Map.Entry)var4.next();
                    result.append(tran2Xml((String)entry.getKey(), entry.getValue()));
                }
            }
        } else {
            result.append(tranString(value));
        }

        result.append("</" + key + ">");
        return result.toString();
    }
}
